/**
  ******************************************************************************
  * @file    AT25DF041.c 
  * @author  Thongnh
  * @version V1.0
  * @date    18-07-2011
  * @brief   Main process of flash AT25DF041
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, XICUTELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Xicutelectronics</center></h2>
  ******************************************************************************
  */ 
#include "StrLib.h"
#include "AT25DF041.h"
#include "Config.h"

cConfig::sConfig cConfig::s_Config;


void cConfig::InitReset()
{
    //cFlash::EraseBlock(cFlash::BLOCK_ERASE_4K, CONFIG_BLOCK_ADDR);
    LoadConfig();
    if ( 0xFFFF == s_Config.m_TotalUser && 0xFFFFFFFF == s_Config.m_LEDOnTime && 0xFFFF == s_Config.m_Port )
    {
        s_Config.m_LEDOnTime = DEFAULT_LED_TIME_ON;
        s_Config.m_TotalUser = DEFAULT_TOTAL_USER;
        s_Config.m_IP[0] = DEFAULT_IP_ADDR_0;
        s_Config.m_IP[1] = DEFAULT_IP_ADDR_1;
        s_Config.m_IP[2] = DEFAULT_IP_ADDR_2;
        s_Config.m_IP[3] = DEFAULT_IP_ADDR_3;
        s_Config.m_Port  = DEFAULT_PORT;
    }
}


bool cConfig::SaveConfig()
{
    cFlash::EraseBlock(cFlash::BLOCK_ERASE_4K, CONFIG_BLOCK_ADDR);
    for (U16 i=0; i<10000; i++);
    cFlash::Write(CONFIG_BLOCK_ADDR, (U8*)(&s_Config), sizeof(sConfig));
    return (true);
}


bool cConfig::LoadConfig()
{
    cFlash::Read(CONFIG_BLOCK_ADDR, (U8*)(&s_Config), sizeof(sConfig));
    return (true);
}


//////////////////////////////////////////////////////////////////////////////////////
void cConfig::GetTimeZone(U16 *pTimeZoneStart, U16 *pTimeZoneEnd)
{
    for (U8 i=0; i<TIME_ZONE_MAX; i++)
    {
        pTimeZoneStart[i] = s_Config.m_pTimeZoneStart[i];
        pTimeZoneEnd[i]   = s_Config.m_pTimeZoneEnd[i];
    }
}

void cConfig::SetTimeZone(U16 *pTimeZoneStart, U16 *pTimeZoneEnd)
{
    for (U8 i=0; i<TIME_ZONE_MAX; i++)
    {
        s_Config.m_pTimeZoneStart[i] = pTimeZoneStart[i];
        s_Config.m_pTimeZoneEnd[i]   = pTimeZoneEnd[i];
    }
    SaveConfig();
}

//////////////////////////////////////////////////////////////////////////////////////
U8 cConfig::GetLedTimeOn()
{
    return ( (U8)(s_Config.m_LEDOnTime / 1000) );
}

void cConfig::SetLedTimeOn(U8 iLedTimeOn)
{
    s_Config.m_LEDOnTime = iLedTimeOn * 1000;
    SaveConfig();
}

//////////////////////////////////////////////////////////////////////////////////////
void cConfig::GetIPConfig(U8* pIP, U16 &iPort)
{
    cStrLib::Strcpy(pIP, s_Config.m_IP, 4);
    iPort = s_Config.m_Port;
}

void cConfig::SetIPConfig(U8* pIP, U16 iPort)
{
    cStrLib::Strcpy(s_Config.m_IP, pIP, 4);
    s_Config.m_Port = iPort;
    SaveConfig();
}

//////////////////////////////////////////////////////////////////////////////////////
U16 cConfig::GetTotalUser()
{
    return (s_Config.m_TotalUser);
}

void cConfig::SetTotalUser(U16 iTotalUser)
{
    s_Config.m_TotalUser = iTotalUser;
    SaveConfig();
}


// end of file

